from yaml import serialize
from utils.viewset import CustomModelViewSet
from utils.serializers import CustomModelSerializer
from utils.jsonResponse import SuccessResponse,ErrorResponse
from apps.portal.models import Portal


class PortalSerializer(CustomModelSerializer):
    """
    地区-序列化器
    """

    class Meta:
        model = Portal
        fields = "__all__"
        read_only_fields = ["id"]


class PortalCreateUpdateSerializer(CustomModelSerializer):
    """
    地区管理 创建/更新时的列化器
    """

    class Meta:
        model = Portal
        fields = '__all__'


class PortalViewSet(CustomModelViewSet):
    """
    地区管理接口:
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Portal.objects.all().order_by('id')
    serializer_class = PortalSerializer
    filter_fields = ['status','id']
    search_fields = ('name')

    def Portal_root(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        queryset = queryset.filter(parent__isnull=True).order_by('id')
        serializer = PortalSerializer(queryset, many=True)
        return SuccessResponse(data=serializer.data, msg="获取成功")
